/**
 * @开发版权 云南赢中吉洋智能技术有限公司（YNYZ）
 * @项目名称 Main
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2020-05-23
 * @修订日期
 * @描述  Main
 */
package application;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.sql.Connection;
import org.opencv.core.Core;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import application.common.cron4j.*;
import application.common.mapping._MappingKit;
import application.common.util.FiltUtil;
import application.common.util.RouteUtil;
import application.common.util.ShiroUtil;
import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application {

	protected final Log log = Log.getLog(getClass());
	public static DruidPlugin druidPlugin;
	public static ActiveRecordPlugin arp;
	public static Cron4jPlugin cron4jPlugin;

	static {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
		java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
		System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
		System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
		System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "ERROR");
		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "ERROR");
		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "ERROR");
	}

	@Override
	public void start(Stage stage) {
		try {
			ShiroUtil.setProcessId(getProcessID());
			ShiroUtil.setMainProcess(this);
			RouteUtil.init(stage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void init() throws Exception {
		super.init();
		String path=String.format("%sresources/cabinet.db", FiltUtil.getPath());
		druidPlugin = new DruidPlugin("jdbc:sqlite:"+path, "", "");
		druidPlugin.setDriverClass("org.sqlite.JDBC");
		druidPlugin.setLogAbandoned(true);
		druidPlugin.setTestWhileIdle(true);
		druidPlugin.setRemoveAbandoned(true);
		
		arp = new ActiveRecordPlugin(druidPlugin);
		arp.setShowSql(false);
		arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
		arp.getEngine().setToClassPathSourceFactory();
		arp.setDialect(new Sqlite3Dialect());
		_MappingKit.mapping(arp);
		druidPlugin.start();
		arp.start();
		
		
		Prop p = PropKit.use(new File(String.format("%sresources/app-config-dev.txt", FiltUtil.getPath())));

		
		cron4jPlugin = new Cron4jPlugin();
		cron4jPlugin.addTask(p.get("cabinetTask", "*/1 * * * *"), new CabinetTask());
		cron4jPlugin.addTask(p.get("boardTask", "*/1 * * * *"), new BoardTask());
		cron4jPlugin.addTask(p.get("configTask", "*/1 * * * *"), new ConfigTask());
		cron4jPlugin.addTask(p.get("faultTask", "*/1 * * * *"), new FaultTask());
		cron4jPlugin.addTask(p.get("pictureTask", "*/1 * * * *"), new PictureTask());
		cron4jPlugin.addTask(p.get("storeBoxLogTask", "*/1 * * * *"), new StoreBoxLogTask());
		cron4jPlugin.addTask(p.get("storeBoxTask", "*/1 * * * *"), new StoreBoxTask());
		cron4jPlugin.addTask(p.get("storeLogTask", "*/1 * * * *"), new StoreLogTask());//1 */1 * * *
		cron4jPlugin.addTask(p.get("storeMapTask", "*/1 * * * *"), new StoreMapTask());
		cron4jPlugin.addTask(p.get("storeTask", "*/1 * * * *"), new StoreTask());
		cron4jPlugin.addTask(p.get("storeUserTask", "*/1 * * * *"), new StoreUserTask());
		cron4jPlugin.addTask(p.get("userTask", "*/1 * * * *"), new UserTask());
		cron4jPlugin.addTask(p.get("videoTask", "*/10 * * * *"), new VideoTask());
		cron4jPlugin.addTask(p.get("fprTask", "*/10 * * * *"), new FprTask());
		cron4jPlugin.start();
	}

	@Override
	public void stop() throws Exception {
		super.stop();
		if (arp != null) {
			arp.stop();
		}
		if (druidPlugin != null) {
			druidPlugin.stop();
		}
		if (cron4jPlugin != null) {
			cron4jPlugin.stop();
		}
		Runtime.getRuntime().exec("taskkill /F /PID " + ShiroUtil.getProcessId());
		System.exit(0);
	}
	
	public static final int getProcessID() {
		RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
		return Integer.valueOf(runtimeMXBean.getName().split("@")[0]).intValue();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
